package com.example.griddemo.api;

import com.example.griddemo.interfaces.ResponseCallback;
import com.example.griddemo.viewmodel.Category;
import com.example.griddemo.viewmodel.CategoryDetail;
import com.example.griddemo.viewmodel.CategoryResponse;
import com.example.griddemo.viewmodel.Goods;
import com.example.griddemo.viewmodel.GoodsListResponse;
import com.example.griddemo.viewmodel.HomeData;
import com.example.griddemo.viewmodel.ResponseData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.List;

public class ApiService {
    private static final String baseURL = "http://hmapp.net:8888/";

    // 获取首页数据
    public static void GetHome(ResponseCallback<ResponseData<HomeData>> callback) {
        OkGo.<String>get(baseURL + "wx/home/index")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<HomeData> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<HomeData>>(){}.getType()
                        );
                        callback.onSuccess(result);
                    }
                });
    }

    // 获取分类数据
    public static void getCategoryData(ResponseCallback<ResponseData<CategoryResponse>> callback) {
        OkGo.<String>get(baseURL + "wx/catalog/index")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CategoryResponse> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<CategoryResponse>>(){}.getType()
                        );
                        callback.onSuccess(result);
                    }
                });
    }

    // 获取子分类数据
    public static void getSubCategoryData(int categoryId, ResponseCallback<ResponseData<CategoryResponse>> callback) {
        OkGo.<String>get(baseURL + "wx/catalog/current")
                .params("id", categoryId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CategoryResponse> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<CategoryResponse>>(){}.getType()
                        );
                        callback.onSuccess(result);
                    }
                });
    }

    public static void getCategoryDetail(int categoryId, ResponseCallback<ResponseData<CategoryDetail>> callback) {
        OkGo.<String>get(baseURL + "wx/goods/category")
                .params("id", categoryId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CategoryDetail> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<CategoryDetail>>(){}.getType()
                        );
                        callback.onSuccess(result);
                    }
                });
    }


    public static void getGoodsListByCategory(int categoryId, ResponseCallback<ResponseData<CategoryDetail>> callback) {
        OkGo.<String>get(baseURL + "wx/goods/category")
                .params("id", categoryId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            ResponseData<CategoryDetail> result = new Gson().fromJson(
                                    response.body(),
                                    new TypeToken<ResponseData<CategoryDetail>>(){}.getType()
                            );
                            if (result != null && result.getErrno() == 0) {
                                callback.onSuccess(result);
                            } else {
                                callback.onError(result != null ? result.getErrmsg() : "数据解析失败");
                            }
                        } catch (Exception e) {
                            callback.onError("数据解析异常: " + e.getMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onError("网络请求失败: " + response.getException().getMessage());
                    }
                });
    }
    public static void getGoodsList(int categoryId, int page, int limit, ResponseCallback<ResponseData<GoodsListResponse>> callback) {
        OkGo.<String>get(baseURL + "wx/goods/list")
                .params("categoryId", categoryId)
                .params("page", page)
                .params("limit", limit)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            ResponseData<GoodsListResponse> result = new Gson().fromJson(
                                    response.body(),
                                    new TypeToken<ResponseData<GoodsListResponse>>(){}.getType()
                            );
                            if (result != null && result.getErrno() == 0) {
                                callback.onSuccess(result);
                            } else {
                                callback.onError(result != null ? result.getErrmsg() : "数据解析失败");
                            }
                        } catch (Exception e) {
                            callback.onError("数据解析异常: " + e.getMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        callback.onError("网络请求失败: " + response.getException().getMessage());
                    }
                });
    }
}